<template>
  <div class="layout-container">
    <div class="header">
      <div class="header-content">
        <div class="header-left">
          <h2>短链</h2>
          <el-menu
            mode="horizontal"
            :router="true"
            :default-active="route.path"
            class="header-menu"
          >
            <el-menu-item index="/dashboard/links">
              <el-icon><Link /></el-icon>
              我的短链接
            </el-menu-item>
            <el-menu-item index="/dashboard/tags">
              <el-icon><Collection /></el-icon>
              标签管理
            </el-menu-item>
            <el-menu-item index="/dashboard/files">
              <el-icon><Files /></el-icon>
              文件管理
            </el-menu-item>
            <el-menu-item index="/tools">
              <el-icon><Tools /></el-icon>
              工具箱
            </el-menu-item>
          </el-menu>
        </div>
        
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="user-info">
              <el-avatar :size="32" class="user-avatar">
                {{ (userInfo.userName || userInfo.userAccount || '未知')[0] }}
              </el-avatar>
              <span class="username">{{ userInfo.userName || userInfo.userAccount }}</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </div>
    
    <div class="main-container">
      <router-view></router-view>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Link, Collection, Files, Tools } from '@element-plus/icons-vue'
import { userApi } from '../api/user'

const route = useRoute()
const router = useRouter()
const userInfo = ref(JSON.parse(localStorage.getItem('userInfo') || '{}'))

onMounted(async () => {
  try {
    const res = await userApi.getCurrentUser()
    userInfo.value = res.data
    localStorage.setItem('userInfo', JSON.stringify(res.data))
  } catch (error) {
    ElMessage.error('获取用户信息失败')
    // 如果获取用户信息失败，可能是未登录，跳转到登录页
    router.replace('/login')
  }
})

const handleCommand = (command) => {
  if (command === 'logout') {
    ElMessageBox.confirm('确认退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(async () => {
      try {
        await userApi.logout()
        localStorage.clear()
        router.replace('/login')
        ElMessage.success('退出成功')
      } catch (error) {
        ElMessage.error('退出失败')
      }
    })
  }
}
</script>

<style scoped>
.layout-container {
  width: 100%;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f5f7fa;
}

.header {
  width: 100%;
  background-color: #fff;
  border-bottom: 1px solid #ebeef5;
  position: fixed;
  top: 0;
  z-index: 1000;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.main-container {
  margin-top: 60px;
  flex: 1;
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
}

:deep(.main-container > *) {
  width: 100%;
  max-width: 1400px;
}

@media screen and (max-width: 1440px) {
  :deep(.main-container > *) {
    max-width: 100%;
    padding: 0 20px;
  }
}

@media screen and (max-width: 768px) {
  .main-container {
    padding: 10px;
  }
  
  :deep(.main-container > *) {
    padding: 0 10px;
  }

  .header-left {
    gap: 20px;
  }
  
  :deep(.el-menu--horizontal .el-menu-item) {
    padding: 0 10px;
  }
}

.header-content {
  height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 40px;
}

.header-left h2 {
  margin: 0;
  font-size: 20px;
  color: #1890ff;
  font-weight: 600;
}

.header-menu {
  border: none;
  background: transparent;
}

:deep(.el-menu--horizontal) {
  border: none;
  
  .el-menu-item {
    height: 60px;
    line-height: 60px;
    border: none !important;
    color: #606266;
    
    &:hover, &.is-active {
      color: #1890ff;
      background-color: transparent;
    }
    
    .el-icon {
      margin-right: 4px;
    }
  }
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.3s;
  
  &:hover {
    background-color: #f5f7fa;
  }
}

.user-avatar {
  background-color: #1890ff;
  color: #fff;
}

.username {
  font-size: 14px;
  color: #606266;
}
</style> 